#include "misc_utils.h"
#include "stddef.h"
#include <stdio.h>

typedef struct NODE
{
    LIST_NODE_T header;
    int var;
} NODE;

struct NODE* new_node(int var)
{
    struct NODE* node = (struct NODE*)malloc(sizeof(struct NODE));
    node->var = var;
    return node;
}

void free_node(struct NODE* node)
{
    free(node);
}

void test_list()
{
    LIST_T list;
    struct NODE* node, *node5 = NULL;
    LIST_NODE_T* ln;
    int i = 0;

    list_init(&list);
    for (i = 0; i < 10; i++)
    {
        node = new_node(i);
        if (node->var == 5)
            node5 = node;
        list_add_head(&list, &node->header);
    }

    for (i = 0; i < 10; i++)
    {
        node = new_node(i + 10);
        if (i & 1)
            list_insert_after(&list, &node->header, &node5->header);
        else
            list_insert_before(&list, &node->header, &node5->header);
    }

    for (ln = list.head; ln != NULL; ln = ln->next)
    {
        node = CONTAINER_PTR(NODE, header, ln);
        printf("var=%d\n", node->var);
    }

    while (list.size > 0)
    {
        node = CONTAINER_PTR(NODE, header, list.head);
        list_remove_head(&list);
        free_node(node);
    }
}
